package com.goodluck.goodluckim.ui.msg.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.ViewGroup;

import com.goodluck.goodluckim.R;
import com.goodluck.goodluckim.ui.BaseActivity;

/**
 * Created by wyj on 2018/9/7.
 */

public class FragmentActivity extends BaseActivity {

    private Fragment fragment;

    public static void goToFragment(Context context, Class<?> fragment, Bundle bundle) {
        Intent intent = new Intent(context, FragmentActivity.class);
        if (bundle == null) {
            bundle = new Bundle();
        }
        bundle.putSerializable("class", fragment);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team);
        initView();
    }

    private void initView() {
        try {
            Intent intent = getIntent();
            Class<?> clsName = (Class<?>) intent.getSerializableExtra("class");
            if (clsName == null) {
                finish();
                return;
            }
            fragment = (Fragment) clsName.newInstance();
            fragment.setArguments(getIntent().getExtras());
        } catch (Exception e) {
            finish();
            return;
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        View view = fragment.getView();
        if (view != null && view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeAllViews();
        }
        fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commitAllowingStateLoss();
    }
}
